<?php
/**
 * Created by PhpStorm.
 * User: Donal
 * Date: 16/4/12
 * Time: 下午10:07
 */
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
    public function registerUser(){
        $name = I('param.name');
        $pass = I('param.pass');
        $nickname = I('param.nickname');
        $phone = I('param.phone');
        $email = I('param.email');
        $sex = I('param.sex');
        $departId = I('param.departId');
        $avatar = I('param,avatar');
        $status = I('param.status');
        $domain = I('param.domain');
        $User = D('IMUser');
        $users = $User->where("name='%s'", $name)->select();
        if (empty($users)) {
            $salt = rand()%10000;
            $params = array(
                'sex'=>$sex,
                'name'=>$name,
                'domain'=>$domain,
                'nick'=>$nickname,
                'password'=>md5(md5($pass).$salt),
                'salt'=>$salt,
                'phone'=>$phone,
                'email'=>$email,
                'avatar'=>$avatar,
                'departId'=>$departId,
                'status'=>$status,
                'created'=>time(),
                'updated'=>time()
            );
            $result = $User->add($params);
            if ($result) {
                $data['status']  = 0;
                $data['msg'] = '账号注册成功了';
                $this->ajaxReturn($data);
            }
            else {
                $data['status']  = 1;
                $data['msg'] = '账号注册失败';
                $this->ajaxReturn($data);
            }
        }
        else {
            $data['status']  = 2;
            $data['msg'] = '已有账号了';
            $this->ajaxReturn($data);
        }

    }

    public function resetUserPassword(){
        $uid = I('param.uid');
        $oldPass = I('param.oldpass');
        $newPass = I('param.newpass');
        $User = D('IMUser');
        $users = $User->where("id=%d", $uid)->select();
        if (empty($users)) {
            $data['status']  = 1;
            $data['msg'] = '没有这个用户';
            $this->ajaxReturn($data);
        }
        else {
            $currentUser = $users[0];
            $salt = $currentUser['salt'];
            $password = md5(md5($oldPass).$salt);
            if ($password == $currentUser['password']) {
                $newSalt = rand()%10000;
                $newPassword = md5(md5($newPass).$newSalt);
                $para['password'] = $newPassword;
                $para['salt'] = $newSalt;
                if ($User->where("id=%d", $uid)->save($para)) {
                    $data['status']  = 0;
                    $data['msg'] = '修改密码成功';
                    $this->ajaxReturn($data);
                }
                else {
                    $data['status']  = 3;
                    $data['msg'] = '修改密码失败';
                    $this->ajaxReturn($data);
                }

            }
            else {
                $data['status']  = 2;
                $data['msg'] = '密码不对';
                $data['salt'] = $salt;
                $data['pass'] = $password;
                $this->ajaxReturn($data);
            }
        }
    }
    
    function updateUserPush() {
        $uid = I('param.uid');
        $clientId = I('param.client_id');
        $platform = I('param.platform');
        $token = I('param.token');
        $GePush = D('IMGetuiPush');
        if ($platform == 0) {
            $params = array(
                'uid'=>$uid,
                'client_id'=>$clientId,
                'platform'=>$platform
            );
        }
        else {
            $params = array(
                'uid'=>$uid,
                'client_id'=>$clientId,
                'platform'=>$platform,
                'token'=>$token
            );
        }
        $result = $GePush->add($params, '', true);
        if ($result) {
            $data['status']  = 0;
            $data['msg'] = '更新推送id成功';
            $this->ajaxReturn($data);
        }
        else {
            $data['status']  = 1;
            $data['msg'] = '更新推送id失败';
            $this->ajaxReturn($data);
        }
    }

    public function test()
    {
        $uid = I('param.uid');
        $tid = I('param.tid');
        $pushManager = new \Home\Lib\PushManager();
        $pushManager->pushMessageToUid($tid, $pushManager->IGtNotificationTemplateApplyFriend());
        $msg['request_uid'] = $uid;
        $msg['request_avatar'] = $uid;
        $msg['request_nickname'] = $uid;
        $msg['request_msg'] = $uid;
        $pushManager->pushTransimisionMessageToUid($tid, $pushManager->IGtTransmissionTemplateApplyFriend(json_encode($msg)));
    }
}